package com.macro.mall.portal.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.api.ResultCode;
import com.macro.mall.common.exception.Asserts;
import com.macro.mall.common.utils.Assist;
import com.macro.mall.mapper.RecommendInfoMapper;
import com.macro.mall.model.RecommendInfo;
import com.macro.mall.model.UmsMember;
import com.macro.mall.portal.domain.MemberDetails;
import com.macro.mall.portal.service.RecommendInfoService;
import com.macro.mall.portal.service.UmsMemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * RecommendInfo的服务接口的实现类
 *
 * @author gaodx
 */
@Slf4j
@Service
public class RecommendInfoServiceImpl implements RecommendInfoService {

    private boolean isDebugEnabled;

    @Autowired
    private RecommendInfoMapper recommendInfoMapper;
    @Autowired
    private UmsMemberService UmsMemberService;
    // TODO 当你看到这个方法时你应该创建一个工具类做通用的方法,定义自己的返回格式化

    @Override
    public List<RecommendInfo> find(Integer sourceId, String sourceType, String channel, String name, Integer pageNum, Integer pageSize, String orderBy) {
        //TODO这里可以做通过Assist做添加查询
        List<RecommendInfo> result = null;
        Assist assist = new Assist();
        try {
            assist.andEq("status", "1");
            if (!StrUtil.isBlankIfStr(sourceId)) {
                assist.andEq("source_id", sourceId);
            }
            if(StrUtil.isNotEmpty(sourceType)) {
                String[] split = sourceType.split(",");
                String t = "";
                for(String type : split) {
                    t+="'"+type+"',";
                }
                t.substring(0,t.lastIndexOf(","));
                assist.andIn("source_type",t);
            }
            UmsMember currentMember = getCurrentMember();
            if("02".equals(currentMember.getUserType())) {
                assist.andNeq("source_type", "02");
            }
            if (StrUtil.isNotEmpty(channel)) {
                assist.andEq("channel", channel);
            }
            if (StrUtil.isNotEmpty(name)) {
                assist.andLike("name", "%" + name + "%");
            }
            if (StrUtil.isNotEmpty(orderBy)) {
                PageHelper.orderBy(orderBy);
            }
            PageHelper.startPage(pageNum, pageSize);
            assist.setOrder(Assist.order("release_time", false));
            result = recommendInfoMapper.selectRecommendInfo(assist);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行获取RecommendInfo数据集-->结果:", result);
        }
        return result;
    }

    @Override
    public RecommendInfo findOne(Integer id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过RecommendInfo的id获得RecommendInfo对象-->失败:id不能为空");
            }
            Asserts.fail("执行通过RecommendInfo的id获得RecommendInfo对象-->失败:id不能为空");
        }
        RecommendInfo result = null;
        try {
            result = recommendInfoMapper.selectRecommendInfoById(id);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过RecommendInfo的id获得RecommendInfo对象-->结果:", result);
        }
        return result;
    }

    @Override
    public CommonResult saveNotNull(RecommendInfo value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行将RecommendInfo中属性值不为null的数据保存到数据库-->失败:对象不能为空");
            }
            Asserts.fail("失败:对象不能为空");
        }
        int result = 0;
        try {
            result = recommendInfoMapper.insertNotNullRecommendInfo(value);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行将RecommendInfo中属性值不为null的数据保存到数据库-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult updateNotNullById(RecommendInfo value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行通过RecommendInfo的id更新RecommendInfo中属性不为null的数据-->失败:对象为null");
            }
            Asserts.fail("执行通过RecommendInfo的id更新RecommendInfo中属性不为null的数据-->失败:对象为null");
        }
        int result = 0;
        try {
            result = recommendInfoMapper.updateNotNullRecommendInfoById(value);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过RecommendInfo的id更新RecommendInfo中属性不为null的数据-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult deleteById(Integer id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过RecommendInfo的id删除RecommendInfo-->失败:id不能为空");
            }
            Asserts.fail("执行通过RecommendInfo的id删除RecommendInfo-->失败:id不能为空");
        }
        int result = 0;
        try {
            result = recommendInfoMapper.deleteRecommendInfoById(id);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过RecommendInfo的id删除RecommendInfo-->结果:", result);
        }
        return CommonResult.success(null);
    }

    public UmsMember getCurrentMember() {
        SecurityContext ctx = SecurityContextHolder.getContext();
        Authentication auth = ctx.getAuthentication();
        MemberDetails memberDetails = (MemberDetails) auth.getPrincipal();
        return UmsMemberService.getById(memberDetails.getUmsMember().getId());
    }
}